{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": [],
      "authorship_tag": "ABX9TyMNbR5m2ZHTexdCdR4aNI67",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ktynski/Marketing_Automations_Notebooks_With_GPT/blob/main/Prompt_Chaining_Instant_Content_Plan_(Public).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "http://www.Frac.tl - My Agency, we earn press and massively improve the ranking potential of our client's domains through a data-journalism style approach to earned media. We are excited about the limitless possibilities of AI enabled processes in Content Marketing, SEO, and PR. Email me at Kristin@frac.tl if you would like to learn more about our services."
      ],
      "metadata": {
        "id": "OTY2ZZ6ZmHN1"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install openai\n",
        "!pip install langchain\n",
        "!pip install asyncio"
      ],
      "metadata": {
        "id": "6lVMdHPnR4Pl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "import pandas as pd\n",
        "import openai\n",
        "import re\n",
        "import concurrent.futures\n",
        "\n",
        "# Set up OpenAI API key\n",
        "openai.api_key = \"Enter your API Key\"\n",
        "\n",
        "\n",
        "def generate_article_title(topic, subtopic, subsubtopic):\n",
        "    prompt = f\"Give an SEO optimized an article title about'{subsubtopic}' under the topic of '{topic}' and the subtopic of '{subtopic}'\"\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=prompt,\n",
        "        max_tokens=800,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.7,\n",
        "    )\n",
        "    while not response.choices[0].text.strip():\n",
        "        response = openai.Completion.create(\n",
        "            engine=\"text-davinci-003\",\n",
        "            prompt=prompt,\n",
        "            max_tokens=100,\n",
        "            n=1,\n",
        "            stop=None,\n",
        "            temperature=0.7,\n",
        "        )\n",
        "    while not response.choices[0].text.strip():\n",
        "        time.sleep(5)\n",
        "    return response.choices[0].text.strip()\n",
        "\n",
        "def generate_subsubtopics(topic, subtopic):\n",
        "    prompt = f\"List 10 SEO Optimized sub-subtopics under the topic of '{topic}' and the subtopic of '{subtopic}'\"\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=prompt,\n",
        "        max_tokens=200,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.7,\n",
        "    )\n",
        "    while not response.choices[0].text.strip():\n",
        "        response = openai.Completion.create(\n",
        "            engine=\"text-davinci-003\",\n",
        "            prompt=prompt,\n",
        "            max_tokens=200,\n",
        "            n=1,\n",
        "            stop=None,\n",
        "            temperature=0.7,\n",
        "        )\n",
        "    subsubtopics = re.findall(r\"\\d+\\. (.*)\", response.choices[0].text.strip())\n",
        "    return subsubtopics\n",
        "\n",
        "def generate_subtopics(topic):\n",
        "    prompt = f\"List 10 seo optimized subtopics under the topic of '{topic}'\"\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=prompt,\n",
        "        max_tokens=200,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.7,\n",
        "    )\n",
        "    while not response.choices[0].text.strip():\n",
        "        response = openai.Completion.create(\n",
        "            engine=\"text-davinci-003\",\n",
        "            prompt=prompt,\n",
        "            max_tokens=200,\n",
        "            n=1,\n",
        "            stop=None,\n",
        "            temperature=0.7,\n",
        "        )\n",
        "    subtopics = re.findall(r\"\\d+\\. (.*)\", response.choices[0].text.strip())\n",
        "    return subtopics\n",
        "\n",
        "def generate_data(topic):\n",
        "    subtopics = generate_subtopics(topic)\n",
        "    results = []\n",
        "    with concurrent.futures.ThreadPoolExecutor() as executor:\n",
        "        subsubtopic_futures = {executor.submit(generate_subsubtopics, topic, subtopic): subtopic for subtopic in subtopics}\n",
        "        for future in concurrent.futures.as_completed(subsubtopic_futures):\n",
        "            subtopic = subsubtopic_futures[future]\n",
        "            subsubtopics = future.result()\n",
        "            for subsubtopic in subsubtopics:\n",
        "                results.append((topic, subtopic, subsubtopic))\n",
        "\n",
        "    with concurrent.futures.ThreadPoolExecutor() as executor:\n",
        "        article_title_futures = {executor.submit(generate_article_title, topic, subtopic, subsubtopic): (subtopic, subsubtopic) for (topic, subtopic, subsubtopic) in results}\n",
        "        for future in concurrent.futures.as_completed(article_title_futures):\n",
        "            (subtopic, subsubtopic) = article_title_futures[future]\n",
        "            article_title = future.result()\n",
        "            while not article_title:\n",
        "                article_title = generate_article_title(topic, subtopic, subsubtopic)\n",
        "            results.append((topic, subtopic, subsubtopic, article_title))\n",
        "\n",
        "    df = pd.DataFrame(results, columns=[\"topic\", \"subtopic\", \"subsubtopic\", \"article_title\"])\n",
        "    return df\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    topic = \"Fishing\"\n",
        "    df = generate_data(topic)\n",
        "    for i in range(200):\n",
        "        article_title = generate_article_title(df.loc[i, 'topic'], df.loc[i, 'subtopic'], df.loc[i, 'subsubtopic'])\n",
        "        df.loc[i, 'article_title'] = article_title\n",
        "    print(df)"
      ],
      "metadata": {
        "id": "Havir0g_UQyT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df.to_csv(\"fishing.csv\")"
      ],
      "metadata": {
        "id": "uI1gu1zE1ia8"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}